yjx.controller('pdfDetailController', ['$rootScope', '$scope', '$stateParams', '$fetch', '$timeout', 'ipCookie', '$state','modalService', 'wxShareService','$locals',
    function ($rootScope, $scope, $stateParams, $fetch, $timeout, ipCookie, $state,modalService,wxShareService,$locals) {
        var id = $stateParams.id;
        var pdfUrl = $stateParams.pdfUrl;
        var pdfScale = 1    ; // make pdfScale a global variable
        var pdfmodal = document.getElementById('pdfModal');
        var canvasp = document.getElementById('pdf-container');
        var shownPdf; // another global we'll use for the buttons
        var pdfScrollTop = 0;
        var lastPdfUrl = '';
        $scope.pdfLoading = true;
        $scope.isZooming = false;
        var pzC = pdfmodal.closest('.scrollable-content');
        $scope.hideTab = true;
        // alert("pdf页=="+id);
        //var url = './helloworld.pdf' // PDF to load: change this to a file that exists;

        function renderPage(page, isSingle) {
            var scale = pdfScale; // render with global pdfScale variable
            var viewport = page.getViewport(scale);
            var canvas;
            if (isSingle) {
                canvas = canvasp
            } else {
                canvas = document.createElement('canvas');
                canvas.className = 'pagesCanvas';
            }
            var context = canvas.getContext('2d');
            canvas.height = viewport.height;
            canvas.width = viewport.width;
            pdfmodal.appendChild(canvas);
            var renderContext = {
                canvasContext: context,
                viewport: viewport
            };
            $timeout(function () {
                page.render(renderContext);
                $scope.pdfLoading = false;
                $scope.isZooming = false;
                if (pdfScrollTop) {
                    angular.element(pzC).css('scrollTop', pdfScrollTop);//将滚动条设置到当前浏览位置
                }
            })
        }

        function displayPage(pdf) {
            if (pdf.numPages > 1) {
                canvasp.style.display = "none";
                angular.element(document.querySelectorAll('.pagesCanvas')).detach();
                for (var i = 1; i <= pdf.numPages; i++) {
                    pdf.getPage(i).then(function getPage(page) {
                        renderPage(page, false);
                    });
                }
            } else {
                canvasp.style.display = "inline-block";
                pdf.getPage(1).then(function getPage(page) {
                    renderPage(page, true);
                });
            }
        }

        function showPdfAct(url) {
            /*进入页面PDF预览初始化内容*/
            ipCookie('pdfScrollTop', 0);//清除上个PDF的滚动条缓存
            $timeout(function () {
                angular.element(pzC).css('height', document.body.offsetHeight);
            })
            PDFJS.workerSrc = 'common/libs/pdf.worker.js';
            PDFJS.getDocument(url).then(function getPdfHelloWorld(pdf) {
                displayPage(pdf);
                shownPdf = pdf;
            });
        }
        $scope.pdfView = function(pdfUrl){
            if(pdfUrl){
                // console.log($scope.$parent.pdfUrl;);
                if(lastPdfUrl != '' && pdfUrl != lastPdfUrl ) {
                    /*清除之前的canvas*/
                    var canvasp = document.getElementById('pdf-container');
                    canvasp.style.display = "none";
                    angular.element(document.querySelectorAll('.pagesCanvas')).detach();
                }
                lastPdfUrl = pdfUrl
                showPdfAct(pdfUrl)
            }
        }
        /*必须是子路由 这样可点击返回 怎样变换url */
        var  removeWidth = true;
        /*放大查看*/
        $scope.zoomin = function () {
            //if(removeWidth){
            $timeout(function () {
                angular.element(pdfmodal).find('canvas').css('width','auto')
            })
            //}

            removeWidth = false;
            if (!$scope.isZooming) {
                $scope.isZooming = true;
                resetScrollTop(true);
                pdfScale = pdfScale + 0.25;
                displayPage(shownPdf);
            }
        }
        /*缩小查看*/
        $scope.zoomout = function () {
            if (!$scope.isZooming) {
                $scope.isZooming = true;
                if (pdfScale <= 0.25) {
                    return;
                }
                resetScrollTop(false);
                pdfScale = pdfScale - 0.25;
                displayPage(shownPdf);
            }

        }
        /*为当前浏览位置重新计算滚动条*/
        function resetScrollTop(isPlus) {
            var pdfScrollTop0 = ipCookie('pdfScrollTop');
            if (pdfScrollTop0 > 10) {
                if (isPlus) {
                    pdfScrollTop = parseInt(pdfScrollTop0 * pdfScale / pdfScale + 0.25)
                } else {
                    pdfScrollTop = parseInt(pdfScrollTop0 * pdfScale / pdfScale - 0.25)
                }
            }
        }
    }])